package com.bbs.action.interceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ValidateInterceptor extends AbstractInterceptor{

	private static final long serialVersionUID = 1L;
	private String actionName;
	private String sessionName;
	private List<String> actionNameList = new ArrayList<String>();

	@Override
	public void init() {
		String[] names = actionName.split(",");
		for(String name : names){
			actionNameList.add(name);
		}
	}
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		ActionProxy proxy = invocation.getProxy();
		String actionName = proxy.getActionName();
		if(actionNameList.contains(actionName)){
			return invocation.invoke();
		}else{
			Map<String, Object> session = ActionContext.getContext().getSession();
			if(session.containsKey(sessionName)){
				return invocation.invoke();
			}
		}
		return "login";
	}

}

